package com.aia.prm.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

import com.aia.prm.service.DefaultServiceImpl;
import com.cfh.util.Tools;

public class CompanyCodes {
	private static final Logger logger = Logger.getLogger(CompanyCodes.class);
	public static final Map<String, String> companyCodeMap = new HashMap<String, String>();
	public static final Map<String, String> cityCodeDict = new HashMap<String, String>();
	public static String defaultCompanyCode = "0986";

	static {
		companyCodeMap.put("SH|SH", "0986");
		companyCodeMap.put("BJ|BJ", "1186");
		companyCodeMap.put("GD|SZ", "1086");
		companyCodeMap.put("GD|GZ", "2586");
		companyCodeMap.put("GD|DG", "2886");
		companyCodeMap.put("GD|FS", "2686");
		companyCodeMap.put("GD|HZ", "2586");
		companyCodeMap.put("GD|JM", "2786");
		companyCodeMap.put("GD|ZQ", "2586");
		companyCodeMap.put("GD|QY", "2586");
		companyCodeMap.put("GD|ZJ", "2586");
		companyCodeMap.put("GD|MM", "2586");
		companyCodeMap.put("GD|ST", "2586");
		companyCodeMap.put("GD|ZH", "2586");
		companyCodeMap.put("GD|ZS", "2586");
		companyCodeMap.put("GD|JY", "2586");
		companyCodeMap.put("GD|SG", "2586");
		companyCodeMap.put("JS|NJ", "1286");
		companyCodeMap.put("JS|SU", "1286");
		companyCodeMap.put("JS|NT", "1286");
		companyCodeMap.put("JS|XZ", "1286");
		companyCodeMap.put("JS|WX", "1286");
		companyCodeMap.put("JS|CH", "1286");
		companyCodeMap.put("JS|TZ", "1286");
		companyCodeMap.put("JS|YZ", "1286");
		companyCodeMap.put("JS|YC", "1286");
		companyCodeMap.put("JS|LY", "1286");
	}

	static {
		cityCodeDict.put("上海-上海", "SH|SH|0986|上海|上海");
		cityCodeDict.put("北京-北京", "BJ|BJ|1186|北京|北京");
		cityCodeDict.put("北京市-北京市", "BJ|BJ|1186|北京|北京");
		cityCodeDict.put("广东-深圳", "GD|SZ|1086|深圳|深圳");
		cityCodeDict.put("深圳-深圳", "GD|SZ|1086|深圳|深圳");
		cityCodeDict.put("广东-广州", "GD|GZ|2586|广州|广东");
		cityCodeDict.put("广东-东莞", "GD|DG|2886|东莞-凤岗|广东");
		cityCodeDict.put("广东-佛山", "GD|FS|2686|佛山-三水|广东");
		cityCodeDict.put("广东-惠州", "GD|HZ|2586|惠州-惠城|广东");
		cityCodeDict.put("广东-江门", "GD|JM|2786|江门-市区|广东");
		cityCodeDict.put("广东-肇庆", "GD|ZQ|2586|肇庆-市区|广东");
		cityCodeDict.put("广东-清远", "GD|QY|2586|清远|广东");
		cityCodeDict.put("广东-湛江", "GD|ZJ|2586|湛江|广东");
		cityCodeDict.put("广东-茂名", "GD|MM|2586|茂名|广东");
		cityCodeDict.put("广东-汕头", "GD|ST|2586|汕头|广东");
		cityCodeDict.put("广东-珠海", "GD|ZH|2586|珠海|广东");
		cityCodeDict.put("广东-中山", "GD|ZS|2586|中山|广东");
		cityCodeDict.put("广东-揭阳", "GD|JY|2586|揭阳|广东");
		cityCodeDict.put("广东-韶关", "GD|SG|2586|韶关|广东");
		cityCodeDict.put("江苏-南京", "JS|NJ|1286|南京市区|江苏");
		cityCodeDict.put("江苏-苏州", "JS|SU|1286|苏州市区|江苏");
		cityCodeDict.put("江苏-南通", "JS|NT|1286|南通-南通|江苏");
		cityCodeDict.put("江苏-徐州", "JS|XZ|1286|徐州-徐州|江苏");
		cityCodeDict.put("江苏-无锡", "JS|WX|1286|无锡-无锡|江苏");
		cityCodeDict.put("江苏-常州", "JS|CH|1286|常州-常州|江苏");
		cityCodeDict.put("江苏-泰州", "JS|TZ|1286|泰州-泰州|江苏");
		cityCodeDict.put("江苏-扬州", "JS|YZ|1286|扬州-扬州|江苏");
		cityCodeDict.put("江苏-盐城", "JS|YC|1286|盐城|江苏");
		cityCodeDict.put("江苏-连云港", "JS|LY|1286|连云港|江苏");
	}

	/**
	 * 
	 * @param pro
	 * @param city
	 * @param dist
	 *            No need now
	 * @return
	 */
	public static String pickCompanyCode(String pro, String city, String dist) {
		String result = "";
		// String key = Tools.trimStr(pro) + "|" + Tools.trimStr(city) + "|" +
		// Tools.trimStr(dist);
		// Only pick by two fileds
		String key = Tools.trimStr(pro) + "|" + Tools.trimStr(city);
		if (companyCodeMap.containsKey(key)) {
			result = companyCodeMap.get(key);
		} else {
			logger.error("找不到该城市对应的公司代码:" + key);
			result = defaultCompanyCode;
		}
		return result;
	}

	public static ChinaAddress translateToAddress(String str) throws AddressNotFoundException {
		if (!Tools.hasText(str)) {
			throw new AddressNotFoundException("No text");
		}
		if (!cityCodeDict.containsKey(str)) {
			throw new AddressNotFoundException("Can not found it");
		}
		ChinaAddress chinaAddress = null;
		try {
			String value = cityCodeDict.get(str);
			String[] arr = value.split("\\|");
			Assert.notNull(arr);
			Assert.isTrue(arr.length == 5);
			chinaAddress = new ChinaAddress();
			chinaAddress.setProvinceCode(arr[0]);
			chinaAddress.setCityCode(arr[1]);
			chinaAddress.setCompanyCode(arr[2]);
			chinaAddress.setDistCode(arr[3]);
			chinaAddress.setProviceName(arr[4]);
			return chinaAddress;
		} catch (Exception e) {
			throw new AddressNotFoundException("Error config", e);
		}
	}

	static public void main(String[] args) throws AddressNotFoundException {
		ChinaAddress add1 = CompanyCodes.translateToAddress("上海-上海");
		ChinaAddress add2 = CompanyCodes.translateToAddress("江苏-盐城");
		ChinaAddress add3 = CompanyCodes.translateToAddress("江苏-南京");
		System.out.println(add1);
		System.out.println(add2);
		System.out.println(add3);
	}

}
